//
//  TurnOverBalanceTableViewController.m
//  EasyToTesco2.0
//
//  Created by 袖手旁觀 on 16/6/23.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "TurnOverBalanceTableViewController.h"
#import "FirstBalanceTableViewCell.h"
#import "FirstCellTableViewCell.h"
#import "LastBalanceTableViewCell.h"
@interface TurnOverBalanceTableViewController ()<didClickImmediatePayButtonDelegate,UITextFieldDelegate>

@property (nonatomic,strong)NSArray *secondCellArray;

@property (nonatomic,strong)UITextField *inputTextField;

@property (nonatomic,strong)NSDictionary *userInfoData;

@end

static NSString *const firstBalanceReuseIdentifier = @"firstBalance";

static NSString *const secondBalanceReuseIdentifier = @"secondBalance";

static NSString *const lastFirstReuseIdentifier = @"lastFirst";

static NSString *const lastImmediatePayIdentifier = @"ImmediatePay";

@implementation TurnOverBalanceTableViewController

- (NSDictionary *)userInfoData {
    if (!_userInfoData) {
        _userInfoData = [NSDictionary dictionary];
    }
    return _userInfoData;
}

- (NSArray *)secondCellArray {
    if (!_secondCellArray) {
        _secondCellArray = @[
                             @"所属银行",
                             @"真实姓名",
                             @"开户行",
                             @"银行卡尾号",
                             @"营业账户余额"
                             ];
    
    }
    return _secondCellArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.backgroundColor = BACKGROUND_COLOR;
    
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    self.tableView.bounces = NO;
    
    self.tableView.scrollEnabled = NO;
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.title = @"营业额结算";
    
    [self loadUserInfoData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 0) {
        return 1;
    } else if (section == 1) {
        return 5;
    } else {
        return 2;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        FirstBalanceTableViewCell *firstBalance = [tableView dequeueReusableCellWithIdentifier:firstBalanceReuseIdentifier];
        
        if (!firstBalance) {
            firstBalance = [[FirstBalanceTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:firstBalanceReuseIdentifier];
            
            firstBalance.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        firstBalance.nameLabel.text = USERNAME;
        
        return firstBalance;
    } else if (indexPath.section == 1) {
        FirstCellTableViewCell *secondCell = [tableView dequeueReusableCellWithIdentifier:secondBalanceReuseIdentifier];
        
        if (!secondCell) {
            secondCell = [[FirstCellTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:secondBalanceReuseIdentifier];
            
            secondCell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            secondCell.rightLabel.hidden = NO;
            secondCell.inPutTextField.hidden = YES;
            secondCell.rightLabel.textColor = [UIColor blackColor];
        }
        
        secondCell.firstLabel.text = self.secondCellArray[indexPath.row];

        switch (indexPath.row) {
            case 0:
                secondCell.rightLabel.text = [self loadBANKWeithNum: [[self.userInfoData objectForKey:@"yinhang"]integerValue]];
                break;
                
            case 1:
                secondCell.rightLabel.text = [self.userInfoData objectForKey:@"xingming"];
                break;
                
            case 2:
                secondCell.rightLabel.text = [self.userInfoData objectForKey:@"kaihuhang"];
                break;
                
            case 3:
                secondCell.rightLabel.text = [NSString stringWithFormat:@"**** %@",[self loadBankLastFourNum:[self.userInfoData objectForKey:@"zhanghao"]]];
                break;
                
            case 4:
                secondCell.rightLabel.text = [self.userInfoData objectForKey:@"yyye"];
                break;
                
            default:
                break;
        }
        
//        secondCell.rightLabel.text = [self.secondCellArray[indexPath.row] objectForKey:@"right"];
        
        return secondCell;
    } else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            FirstCellTableViewCell *lastFirst = [tableView dequeueReusableCellWithIdentifier:lastFirstReuseIdentifier];
            
            if (!lastFirst) {
                lastFirst = [[FirstCellTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:lastFirstReuseIdentifier];
                
                lastFirst.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            
            lastFirst.firstLabel.text = @"派现金额";
            
            lastFirst.inPutTextField.tag = 101;
//            lastFirst.inPutTextField.delegate = self;
            lastFirst.inPutTextField.placeholder = @"请输入金额";
            lastFirst.inPutTextField.keyboardType = UIKeyboardTypeNumberPad;
            self.inputTextField = lastFirst.inPutTextField;
            
            return lastFirst;
        } else {
            LastBalanceTableViewCell *lastCell = [tableView dequeueReusableCellWithIdentifier:lastImmediatePayIdentifier];
            if (!lastCell) {
                lastCell = [[LastBalanceTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:lastImmediatePayIdentifier];
                lastCell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            
            lastCell.delegate = self;
            
            return lastCell;
        }
    }
    
    return nil;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.inputTextField resignFirstResponder];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 82;
    } else if (indexPath.section == 2 && indexPath.row == 1) {
        return HEIGHT - 430;
//        return HEIGHT - 300;
    } else {
        return 44;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0.001;
    } else {
        return 10;
    }
}

- (void)didClickImmediatePayButton:(UIButton *)immediatePayButton {
    [self.inputTextField resignFirstResponder];
    
    if (self.inputTextField.text.length == 0) {
        
        MBProgressHUD *hud = [MBProgressHUD showMessage:@"请输入金额" toView:self.view dimBackground:NO AndCompleteBlock:^(id object) {
            [self.inputTextField becomeFirstResponder];
        }];
        
        [self.view addSubview:hud];
        
    } else {
        [self didClickImmediatePayButton];
    }
}

- (NSString *)loadBANKWeithNum:(NSInteger)BankNum {
    switch (BankNum) {
        case 1:
            return @"中国农业银行";
            break;
            
        case 2:
            return @"中国工商银行";
            break;
            
        case 3:
            return @"中国建设银行";
            break;
            
        case 4:
            return @"中国银行";
            break;
            
        case 5:
            return @"交通银行";
            break;
            
        case 6:
            return @"招商银行";
            break;
            
        case 7:
            return @"中国民生银行";
            
        default:
            break;
    }
    return @"请确认开户银行";
}

- (NSString *)loadBankLastFourNum:(NSString *)string {
    
    NSLog(@"%@",string);
    NSInteger length = [string length];
    
    NSString *lastString = [string substringWithRange:NSMakeRange(length - 4, 4)];
    NSLog(@"%@",lastString);
    
    return lastString;
    
}


- (void)loadUserInfoData {
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:@{@"username":USERNAME} AndMenthodName:@"AppUser/UserInfo" completeBlock:^(id object) {
        self.userInfoData = [object objectForKey:@"data"];
//        NSLog(@"%@",self.userInfoData);
        [self.tableView reloadData];
    }];
}

- (void)didClickImmediatePayButton {
    
    NSDictionary *params = @{
                             @"username":USERNAME,
                             @"jine":self.inputTextField.text
                             };
    
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"AppHz/Jies" completeBlock:^(id object) {
        [JudgeBackDataCodeMessage JudgeBackDataCodeMessage:@"营业额结算成功" andObjece:object andAddHUDView:self.view andPopViewController:^(id object) {
            self.inputTextField.text = nil;
        }];
    }];
}

@end
